/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package desu.nya.DAO;

import desu.db.HibernateUtil;
import desu.nya.Owner;
import java.sql.SQLException;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.Session;

/**
 *
 * @author Fess
 */
public class OwnerIMPL implements OwnerDAO{
    
    @Override
    public void addOwner(Owner owner) throws SQLException {
        Session session=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(owner);
            session.getTransaction().commit();
        }
        catch(Exception ex){
            System.err.println("Ошибка при вставке владельца");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
    }
    }

    @Override
    public Owner getOwnerByID(int id) throws SQLException {
        Owner owner=null;
        Session session=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            owner=(Owner)session.load(Owner.class,id );
        }
        catch(Exception ex){
            System.err.println("Ошибка при поиске владельца");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
        return owner;
    }

    @Override
    public void updateOwner(int owner_id, Owner owner) throws SQLException {
        Session session=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(owner);
            session.getTransaction().commit();
        }
        catch(Exception ex){
            System.err.println("Ошибка при обновлении владельца");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
    }

    @Override
    public List<Owner> getAllOwner() throws SQLException {
        List<Owner> list=null;
        Session session=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            list=(List<Owner>)session.createCriteria(Owner.class).list();
        }
        catch(Exception ex){
            System.err.println("Ошибка при обновлении владельца");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
        return list;
    }

    @Override
    public void deleteOwner(Owner owner) throws SQLException {
        Session session=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(owner);
            session.getTransaction().commit();
        }
        catch(Exception ex){
            System.err.println("Ошибка при обновлении владельца");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
    }
    
}
